package pt.ipleiria.plp.Lixeiras;

import de.enough.polish.io.Serializable;
import java.util.Calendar;
import java.util.Date;

/**
 * Parser for the Java Date format to Xml DateTime
 * The DateTime is specified on http://www.w3schools.com/Schema/schema_dtypes_date.asp
 * @author Filipe Felisberto e Ricardo Silva
 */
public class LixeiraDate implements Serializable{

    Date date;

    public LixeiraDate() {

        date = new Date();


    }

    public LixeiraDate(String dateXml) {
        try {
            date = parseDate(dateXml);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }



    }

    /**
     * Parses a string with a date field from the webservice and returns a java
     * date field.
     * based on the code on: http://discussion.forum.nokia.com/forum/showthread.php?t=73342
     * @param dateString the string to parse
     * @return the date in java format
     * @throws IllegalArgumentException
     * @throws Exception
     */
    public static Date parseDate(String dateString) throws IllegalArgumentException, Exception {


        if (dateString == null || dateString.equals("")) {
            throw new IllegalArgumentException("Invalid String to Parse as Date - dateString was null or empty");
        }

        int strSize = dateString.length();

        if (strSize < 19) {
            throw new IllegalArgumentException("Invalid String to Parse as Date - dateString invalid string length (" + strSize + ")");
        }

        String yearStr = dateString.substring(0, 4);
        String monthStr = dateString.substring(5, 7);
        String dayStr = dateString.substring(8, 10);
        String hourStr = dateString.substring(11, 13);
        String minuteStr = dateString.substring(14, 16);
        String secondsStr = dateString.substring(17, 19);

        int year = 0;
        int day = 0;
        int month = 0;
        int hour = 0;
        int minute = 0;
        int seconds = 0;

        try {
            year = Integer.parseInt(yearStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + yearStr + "' as a valid year");
        }
        try {
            day = Integer.parseInt(dayStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + dayStr + "' as a valid day");
        }
        try {
            month = Integer.parseInt(monthStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + monthStr + "' as a valid month");
        }

        try {
            hour = Integer.parseInt(hourStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + hourStr + "' as a valid hour");
        }
        try {
            minute = Integer.parseInt(minuteStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + minuteStr + "' as a valid minute");
        }
        try {
            seconds = Integer.parseInt(secondsStr);
        } catch (Exception e) {
            throw new Exception("Could not parse '" + secondsStr + "' as a valid seconds");
        }

        Date date = new Date(0);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, day);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, seconds);

        date = cal.getTime();

        return date;
    }

    /**
     * Parsers the date from the java format into a xml DateTime format
     * @return the converted date
     */
    public String getXmlDate(){

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        StringBuilder str = new StringBuilder();

        str.append(cal.get(Calendar.YEAR));
        str.append("-");
        if(cal.get(Calendar.MONTH)<10)
            str.append("0");
        str.append((cal.get(Calendar.MONTH)+1));
        str.append("-");
        if(cal.get(Calendar.DATE)<10)
            str.append("0");
        str.append(cal.get(Calendar.DATE));
        str.append("T");
        if(cal.get(Calendar.HOUR_OF_DAY)<10)
            str.append("0");
        str.append(cal.get(Calendar.HOUR_OF_DAY));
        str.append(":");
        if(cal.get(Calendar.MINUTE)<10)
            str.append("0");
        str.append(cal.get(Calendar.MINUTE));
        str.append(":");
        if(cal.get(Calendar.SECOND)<10)
            str.append("0");
        str.append(cal.get(Calendar.SECOND));


        return str.toString();
    }

    public String toString() {
        return getXmlDate();
    }


}
